//
//  timeStampConversionTool.m
//  代码布局
//
//  Created by iOS on 16/8/2.
//  Copyright © 2016年 iOS. All rights reserved.
//

#import "timeStampConversionTool.h"

@implementation timeStampConversionTool
#pragma mark 获取当前时间 年月日 时分秒
+ (NSString *)timestamp
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
  //  [formatter setDateFormat:@"HH:mm:ss"];

    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    [formatter setTimeZone:timeZone];
    
    NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式
    
    NSString *nowtimeStr = [formatter stringFromDate:datenow];//----------将nsdate按formatter格式转成nsstring
    
    return nowtimeStr;
}

#pragma mark 当前时间转换成时间戳
+(NSString*)timestampCode{
    //当前时间转换成时间戳
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval a=[dat timeIntervalSince1970];
    NSString *timeString = [NSString stringWithFormat:@"%0.f", a];//转为字符型
    return timeString;
    
    
}
#pragma mark 时间戳转换成年月日
+ (NSString*)timecodeTowordTime:(NSString*)Timedate{
    NSInteger timeLine = [Timedate integerValue];
    NSDate *time = [NSDate dateWithTimeIntervalSince1970:timeLine];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *timeStr = [dateFormatter stringFromDate:time];
    return timeStr;
}
//算是时间差
+ (NSString *)intervalSinceNow: (NSString *) theDate
{
    NSString *timeString=@"";
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    [formatter setTimeZone:timeZone];
    
    NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式
    
    
    
    NSInteger timeLine = [theDate integerValue];
    NSDate *time = [NSDate dateWithTimeIntervalSince1970:timeLine];
    
    NSTimeInterval cha1 = [time timeIntervalSinceDate: datenow];
    // int allhour = (int)(cha/3600) - 15*3600 - 14*60;
    // NSTimeInterval cha = cha1 - 15*3600 - 14*60 + 12;
    NSTimeInterval cha = cha1;
    int allhour = (int)cha/3600;
    int hour = allhour%24;
    int days = allhour/24;
    int minute = (int)(cha - allhour*3600)/60;
    int second = cha - allhour*3600 - minute*60;
    if(cha <= 0){
        timeString = @"0天00:00:00";
        //     timeString = [NSString stringWithFormat:@"%d天%.2d:%.2d:%.2d",days,hour,minute,second];
        
    }else{
        timeString = [NSString stringWithFormat:@"%d天%.2d:%.2d:%.2d",days,hour,minute,second];
    }
    return timeString;
}
//算是时间差缓存
+ ( int)CacheIntervalSinceNow: (NSString *) theDate
{
    NSString *timeString=@"";
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    [formatter setTimeZone:timeZone];
    
    NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式
    
    
    
    NSInteger timeLine = [theDate integerValue];
    NSDate *time = [NSDate dateWithTimeIntervalSince1970:timeLine];
    
    NSTimeInterval cha = [time timeIntervalSinceDate: datenow];
    cha = - cha;
    int allhour = (int)(cha/3600);
    //    int hour = allhour%24;
    //    int days = allhour/24;
    //    int minute = (int)(cha - allhour*3600)/60;
    //    int second = cha - allhour*3600 - minute*60;
    //    if(cha <= 0){
    //        timeString = @"0天00:00:00";
    //        timeString = [NSString stringWithFormat:@"%d天%.2d:%.2d:%.2d",days,hour,minute,second];
    //
    //    }else{
    //        timeString = [NSString stringWithFormat:@"%d天%.2d:%.2d:%.2d",days,hour,minute,second];
    //    }
    return allhour;
    //  return (int)cha;
}
//发送验证码倒计时
+(void)startTime:(UIButton*)btn {
    
    __block int  timeout_int=60;
    
    dispatch_queue_t queue_global =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0,0,queue_global);
    
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0);
    
    dispatch_source_set_event_handler(_timer, ^{
        
        __weak UIButton *weakself =btn ;
        
        if(timeout_int <= 0){ //倒计时结束，关闭
            
            dispatch_source_cancel(_timer);
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [weakself  setTitle:@"再次发送" forState:UIControlStateNormal];
            });
            weakself.enabled = YES;
        }else{
            weakself.enabled = NO;
            //            int minutes = timeout_int / 60;
            
            int seconds = timeout_int % 61;
            
            NSString *strTime = [NSString stringWithFormat:@"%.2d秒后再次发送", seconds];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置界面的按钮显示
                [weakself  setTitle:strTime forState:UIControlStateNormal];
                //  weakself.numberLabel.text = strTime;
                
            });
            
            timeout_int--;
            
        }
        
    });
    
    dispatch_resume(_timer);
    
    
}

@end
